package com.itboye.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.itboye.shop.base.BaseFragment;
import com.itboye.shop.bean.LunBoBean;
import com.itboye.shop.common.CarouselView;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.util.helper.ImageLoader;
import com.itboye.shop.util.loadutil.Const;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;


/**
 * 首页
 */
public class IndexFragment extends BaseFragment implements Observer {

    TextView tvTitle;
    ImageView back;
    CarouselView lunbo;
    UserPresenter userPresenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_index,null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        back.setVisibility(View.GONE);
        tvTitle.setText("首页");

        userPresenter=new UserPresenter(this);
        userPresenter.getLunBo("6079");
    }

    @Override
    public void update(Observable observable, Object o) {
        ResultEntity resultEntity=handlerError(o);
        if (resultEntity!=null) {
            if (resultEntity.getEventType()== UserPresenter.lunbo_success) {
                List<LunBoBean> lunBoBeanList= (List<LunBoBean>) resultEntity.getData();
                if (lunBoBeanList!=null) {
                    ArrayList<ImageView> arrayBmps = new ArrayList<ImageView>();
                    for (int i = 0; i < lunBoBeanList.size(); i++) {
                        SimpleDraweeView imageView=new SimpleDraweeView(getActivity());
                        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        String m = lunBoBeanList.get(i).getImg();
                        ImageLoader.loadImage(imageView,Const.IMAGE_HEAD + m);
                        arrayBmps.add(imageView);
                        imageView.setTag(i);
                    }
                    lunbo.setImageBitmaps(arrayBmps);
                }

            }else if (resultEntity.getEventType()== UserPresenter.lunbo_success) {

            }
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
    }
}
